﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TomatoClock.Models;
using TomatoClock.ViewModels;

namespace TomatoClock.Views
{
    public partial class RankingControl : UserControl
    {
        private FlowLayoutPanel flowLayoutPanel;
        private MainViewModel viewModel;
        private List<User> users;

        public void setViewModel(MainViewModel v)
        {
            viewModel = v;
        }
        public void setUser(List<User> users)
        {
            this.users = users;
        }
        public RankingControl()
        {
            InitializeComponent();
            InitializeFlowLayoutPanel();
        }
        public RankingControl(List<User> users)
        {
            this.users = users;
            InitializeComponent();
            InitializeFlowLayoutPanel();
            DisplayUser();
        }

        private void InitializeFlowLayoutPanel()
        {
            flowLayoutPanel = new FlowLayoutPanel
            {
                Dock = DockStyle.Fill,
                AutoScroll = true,
                FlowDirection = FlowDirection.TopDown,
                WrapContents = false,
                Padding = new Padding(10) //控制内部距离
            };
            this.Controls.Add(flowLayoutPanel);
        }

        public void DisplayUser()
        {
            flowLayoutPanel.Controls.Clear(); // 清空现有控件

            foreach (var user in users)
            {
                var userPanel = CreateUserPanel(user);
                flowLayoutPanel.Controls.Add(userPanel);
                //MessageBox.Show("生成一个成就");//调试
            }

            flowLayoutPanel.Refresh(); // 刷新控件
        }

        private Panel CreateUserPanel(User user)
        {
            var panel = new Panel
            {
                Width = flowLayoutPanel.ClientSize.Width - 40, // Adjust to the FlowLayoutPanel width
                Height = 20,
                Margin = new Padding(5),
                BorderStyle = BorderStyle.FixedSingle
            };

            var tableLayoutPanel = new TableLayoutPanel
            {
                Dock = DockStyle.Fill,
                ColumnCount = 3, // 3列：图片、描述、解锁时间
            };
            tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33)); // 图片列宽度
            tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 33)); // 描述列宽度
            tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent,33)); // 解锁时间列宽度

            var orderedUsers = users.OrderByDescending(u => u.data.FocusTimeWeekly).ToList();
            int focusTimeRank = orderedUsers.IndexOf(user) + 1;

            var rankLabel = new Label
            {
                Text = "排名",
                AutoSize = true,
                TextAlign = ContentAlignment.MiddleLeft,
            };

            var usernameHeaderLabel = new Label
            {
                Text = "用户名",
                AutoSize = true,
                TextAlign = ContentAlignment.MiddleLeft,
            };

            var focusTimeHeaderLabel = new Label
            {
                Text = "周专注时间",
                AutoSize = true,
                TextAlign = ContentAlignment.MiddleRight,
            };
            var orderLabel = new Label
            {
                Text = $"{focusTimeRank}",
                AutoSize = true,
                TextAlign = ContentAlignment.MiddleLeft,
            };

            var usernameLabel = new Label
            {
                Text = $"{user.UserName}",
                AutoSize = true,
                TextAlign = ContentAlignment.MiddleLeft,
            };

            var focustimeLabel = new Label
            {
                //Text = achievement.CompleteTime.ToString(), // 解锁时间
                Text = $"{user.data.FocusTimeWeekly}",
                AutoSize = true,
                TextAlign = ContentAlignment.MiddleRight,
            };

            // 将控件添加到 TableLayoutPanel 中
            tableLayoutPanel.Controls.Add(focustimeLabel, 0, 0); // 解锁时间
            tableLayoutPanel.Controls.Add(usernameLabel, 0, 0); // 描述
            tableLayoutPanel.Controls.Add(orderLabel, 0, 0); // 图片
            /*tableLayoutPanel.Controls.Add(rankLabel, 0, 0);
            tableLayoutPanel.Controls.Add(usernameHeaderLabel, 0, 0);
            tableLayoutPanel.Controls.Add(focusTimeHeaderLabel, 0, 0);*/
            

            // 将 TableLayoutPanel 添加到 Panel 中
            panel.Controls.Add(tableLayoutPanel);

            return panel;
        }
    }
}
